package funcs

import (
	"bytes"
	"os/exec"
)

/**
这个函数是用来往IPSET中添加要阻止的IP地址
 */
func IPsetAdd(ipset_black_list_name string, bad_ip string) error {

	//组装ipset命令
	//样式：ipset add blacklist_godeny 10.0.1.66

	var buf bytes.Buffer

	buf.WriteString("ipset add ")
	buf.WriteString(ipset_black_list_name)
	buf.WriteString(" ")
	buf.WriteString(bad_ip)
	//	执行命令
	cmd := exec.Command("/bin/bash", "-c", buf.String())
	_, err := cmd.CombinedOutput()

	if err != nil {
		//log.Error("ipset add ",ipset_blacklist," ",ip,"错误：",err.Error())
		return err
	}
	return nil

}

//iptables 的ipst删除操作，目的解除阻止的IP地址
func IPsetDel(ipset_blacklist string, bad_ip string) error {
	//组装ipset命令
	//样式：ipset del blacklist_godeny 10.0.1.66

	var buf bytes.Buffer

	buf.WriteString("ipset del ")
	buf.WriteString(ipset_blacklist)
	buf.WriteString(" ")
	buf.WriteString(bad_ip)
	//	执行命令
	cmd := exec.Command("/bin/bash", "-c", buf.String())
	_, err := cmd.CombinedOutput()

	if err != nil {
		//fmt.Println(err.Error())
		return err
	}
	return nil
}
